package com.hs.carrental.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hs.carrental.common.Result;
import com.hs.carrental.dto.car.CarDeleteDto;
import com.hs.carrental.dto.car.CarsPageDto;
import com.hs.carrental.dto.car.SaveCarDto;
import com.hs.carrental.entity.Car;
import com.hs.carrental.service.ICarService;
import com.hs.carrental.vo.car.CarPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@RestController
@RequestMapping("/carrental/car")
public class CarController {

    @Autowired
    private ICarService carService;

    /**
     * 新增 编辑车辆信息
     * @param saveCarDto
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody SaveCarDto saveCarDto){
        if (carService.save(saveCarDto)){
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * 删除车辆信息
     * @param carDeleteDto
     * @return
     */
    @PostMapping("/delete")
    public Result<Object> delete(@RequestBody CarDeleteDto carDeleteDto){
        //删除 通过主键
        if (carService.removeById(carDeleteDto.getId())){
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * 分页查询车辆信息
     * @return
     */
    @PostMapping("/getCarsPage")
    private Result<IPage<CarPageVo>> getCarsPage(@RequestBody CarsPageDto carsPageDto){
        IPage<CarPageVo> carsPage = carService.getCarsPage(carsPageDto);
        return Result.ok(carsPage);
    }
}
